package com.zggk.iroad.Distribution.distributionlist;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zggk.iroad.R;
import com.zggk.iroad.diease.dieaselist.DistriListBean;

import java.util.ArrayList;

public class DistriAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<DistriListBean.BHLISTBean> listData;
    private ArrayList<Boolean> listSelectTag;
    private boolean isAvaiableChoose = true;

    public DistriAdapter(Context context, ArrayList<DistriListBean.BHLISTBean> listData, boolean isAvaiableChoose) {
        this.context = context;
        this.listData = listData;
        this.isAvaiableChoose = isAvaiableChoose;
        listSelectTag = new ArrayList<>();
        int size = listData.size();
        for (int i = 0; i < size; i++) {
            listSelectTag.add(false);
        }
    }

    public ArrayList<Boolean> getListSelectTag() {
        return listSelectTag;
    }

    public void setListSelectTag(ArrayList<Boolean> listSelectTag) {
        this.listSelectTag = listSelectTag;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.distri_item, parent, false);
            vh.checkBox = (CheckBox) convertView.findViewById(R.id.ck_te);
            vh.stateTxt = (TextView) convertView.findViewById(R.id.zt_te);
            vh.timeTxt = (TextView) convertView.findViewById(R.id.sj_te);
            vh.positionTxt = (TextView) convertView.findViewById(R.id.lx_te);
            vh.typeTxt = (TextView) convertView.findViewById(R.id.lxx_te);
            vh.unitTxt = (TextView) convertView.findViewById(R.id.dw_te);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        if (isAvaiableChoose) {
            final int fpos = position;

            vh.checkBox.setOnCheckedChangeListener(null);
            if (listSelectTag != null && listSelectTag.size() > position) {
                vh.checkBox.setChecked(listSelectTag.get(position));
            }
            vh.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (fpos < listSelectTag.size()) {
                        listSelectTag.set(fpos, isChecked);
                    }
                }
            });
        } else {
        }
        final DistriListBean.BHLISTBean info = listData.get(position);
        String state = info.getBHZT();
        if ("1".equals(state)) {
            vh.stateTxt.setTextColor(context.getResources().getColor(R.color.pf_xtj_text_color));
        } else if ("2".equals(state)) {
            vh.stateTxt.setTextColor(context.getResources().getColor(R.color.pf_xbh_text_color));
        } else if ("3".equals(state)) {
            vh.stateTxt.setTextColor(context.getResources().getColor(R.color.pf_ypf_text_color));
        } else {
            vh.stateTxt.setTextColor(context.getResources().getColor(R.color.tb4));
        }
        String stateStr = getNameByState(state);
        vh.stateTxt.setText(stateStr);
        vh.timeTxt.setText(info.getDCSJ());
        vh.positionTxt.setText(info.getLXBM());
        vh.typeTxt.setText(info.getBHMC());
        vh.unitTxt.setText(info.getGYDWMC());
        return convertView;
    }

    class ViewHolder {
        CheckBox checkBox;
        TextView stateTxt;
        TextView timeTxt;
        TextView positionTxt;
        TextView typeTxt;
        TextView unitTxt;

    }

    public void setAllSelect() {
        if (listSelectTag != null && listSelectTag.size() > 0) {
            for (int i = 0; i < listSelectTag.size(); i++) {
                listSelectTag.set(i, true);
            }
            notifyDataSetChanged();
        }
    }

    public void setAllNoSelect() {
        if (listSelectTag != null && listSelectTag.size() > 0) {
            for (int i = 0; i < listSelectTag.size(); i++) {
                listSelectTag.set(i, false);
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 根据状态获取状态对应的名称
     * 2：新病害，3：已派发 11：已处置
     *
     * @param stateStr
     * @return
     */
    private String getNameByState(String stateStr) {
        String result = "";
        if ("2".equals(stateStr)) {
            result = "新病害";
        } else if ("3".equals(stateStr)) {
            result = "已派发";
        } else if ("11".equals(stateStr)) {
            result = "已处置";
        } else {
            result = "已上传";
        }
        return result;
    }
}




